#include "serialise.h"
#include "../os/os.h"

namespace sleek
{
    namespace core
    {
        namespace serialise
        {
            const char* copyCharAndAdd(const char*chaine,const char*added)
            {
                unsigned long current = os::strsize(chaine), news = os::strsize(added);
                char *strTmp = new char[current+news+1];
                    for( unsigned long i = 0; i<current; ++i)  strTmp[i] = chaine[i];
                    for( unsigned long i = 0; i<news;    ++i)  strTmp[i+current] = added[i];
                    strTmp[current+news] = '\0';
                return strTmp;
            }
            /************************************************************************/
            os::stringc findNextBlocName(os::stringc src,unsigned long pos)
            {
                unsigned long size = 0;
                while(size++ < src.size())
                {
                    size = src.find("[",pos);
                    if(src[size+1] != '/') break;
                }
                return src.subStr(size+1,src.find("]",size)-(size+1));
            }
            unsigned long findBloc(os::stringc src,os::stringc name,unsigned long pos)
            {
                os::stringc result = "[";    result += name;     result += "]";
                return src.find(result.c_str(),pos);
            }
            os::stringc getSerialiseBlocNext(os::stringc src,os::stringc name)
            {
                return getSerialiseBloc(src,name,last_shear);
            }
            os::stringc getSerialiseLigneNext(os::stringc src,os::stringc name)
            {
                return getSerialiseLigne(src,name,last_shear);
            }
            os::stringc getSerialiseBloc(os::stringc src,os::stringc name)
            {
                long ul64 = 0;
                return getSerialiseBloc(src,name,ul64);
            }
            os::stringc getSerialiseLigne(os::stringc src,os::stringc name)
            {
                long ul64 = 0;
                return getSerialiseLigne(src,name,ul64);
            }
            os::stringc getSerialiseBloc(os::stringc src,os::stringc bloc,long &pos)
            {
                unsigned long loc1 = 0, loc2 = 0, loc3 = 0, numberBloc = 0;
                os::stringc result = "[";    result += bloc;     result += "]";
                    loc2 = loc1 = src.find(result.c_str(),pos);
                    /*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*/
                        while(loc2++ < src.size())
                        {
                            loc2 = src.find(result.c_str(),loc2);
                            if(src[loc2+1] != '/') numberBloc++;
                        }
                        result = "[/";    result += bloc;     result += "]";
                        while(loc3++ < src.size() && numberBloc)
                        {
                            loc3 = src.find(result.c_str(),loc3);
                            if(loc3 < 1) break;
                            if(src[loc3+1] == '/') numberBloc--;
                        }
                    /*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*/
                    if(loc3 < 1 && loc1 < 1 && loc3-loc1 < 1) return "";
                    pos = loc3;
                return src.subStr(loc1,(loc3+result.size())-loc1);
            }
            os::stringc getSerialiseLigne(os::stringc src,os::stringc sh,long &pos)
            {
                long loc1 = 0, loc2 = 0;
                os::stringc result = "<";    result += sh;   result += " value = \"";
                    loc1 = src.find(result.c_str(),pos)+result.size();
                result = "\"/>";
                    pos = loc2 = src.find(result.c_str(),loc1);
                if(loc1>-1&&loc2>-1)
                {
                    pos = loc2;
                    return src.subStr(loc1,loc2-loc1);
                }
                return "";
            }
            os::stringc getSerialiseLigneType(os::stringc s)
            {
                long loc1 = s.find(" "), loc2 = s.find(" = \"");
                if(loc1>-1&&loc2>-1) return s.subStr(loc1+1,loc2-loc1+1);
                return "";
            }
            /************************************************************************/
            os::stringc SerialiseLigne(os::stringc name,os::stringc attribute,const char*tablature)
            {
                os::stringc entSrl;
                entSrl += tablature;      entSrl += "<";     entSrl += name;     entSrl += " value = \"";      entSrl += attribute;        entSrl += "\"/>\n";
                return entSrl;
            }
            os::stringc SerialiseBlocBegin(os::stringc name,const char*tablature)
            {
                os::stringc entSrl;
                entSrl += tablature;      entSrl += "[";      entSrl += name;     entSrl += "]\n";
                return entSrl;
            }
            os::stringc SerialiseBlocEnd(os::stringc name,const char*tablature)
            {
                os::stringc entSrl;
                entSrl += tablature;      entSrl += "[/";      entSrl += name;     entSrl += "]\n";
                return entSrl;
            }
            /************************************************************************/
            os::stringc  BoolToChar     (bool src) { if(src) return "true"; return "false"; }
            /****************************************************************/
            bool   CharToBool  (os::stringc src) { if(src == "true") return true; return false; }
            int    CharToInt   (os::stringc src) { return atoi(src.c_str()); }
            double CharToLong  (os::stringc src) { return atol(src.c_str()); }
            float  CharToFloat (os::stringc src) { return atof(src.c_str()); }
            /************************************************************************/
        }
    }
}
